/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-12-12 下午1:55 Created by dwzhang
 */
package com.jade.bss.base.customer.impl;

import java.util.List;
import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.bss.base.customer.CustomerAddress;
import com.jade.bss.base.customer.CustomerAddressDao;
import com.jade.bss.base.customer.CustomerAddressManager;
import com.jade.bss.base.customer.CustomerException;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Order;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 地址管理接口实现。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-12-12 下午1:55
 */
@Named ("bss_customerAddressManager")
public class AddressManagerImpl
        implements CustomerAddressManager
{
    private CustomerAddressDao addressDao;
    private int maxAddressNumber = 10;

    public void setMaxAddressNumber(int maxAddressNumber)
    {
        this.maxAddressNumber = maxAddressNumber;
    }

    @Inject
    public void setAddressDao(@Named ("bss_customerAddressDao") CustomerAddressDao addressDao)
    {
        this.addressDao = addressDao;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void addAddress(CustomerAddress address)
            throws CustomerException
    {
        List<CustomerAddress> addresses = listAddress(address.getCustomerId(), address.getType());
        if (addresses.isEmpty()) {
            address.setDefault(true);
        }
        else if (addresses.size() >= maxAddressNumber) {
            throw new CustomerException("err.bss.customer.address_too_many", "too many addresses");
        }

        //判断地址重复
        //for (CustomerAddress a : addresses) {
        //    if (address.isSameAddress(a)) {
        //        throw new CustomerException("err.bss.customer.address_existed", null, a.getId());
        //    }
        //}
        addressDao.addAddress(address);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void updateAddress(CustomerAddress address)
            throws CustomerException
    {
        CustomerAddress old = addressDao.getAddress(address.getId());
        if (old == null) {
            throw new CustomerException("err.bss.customer.address_not_existed", null, address.getId());
        }
        old.setName(address.getName());
        old.setCompany(address.getCompany());
        old.setMobile(address.getMobile());
        old.setPhone(address.getPhone());
        old.setCountry(address.getCountry());
        old.setProvince(address.getProvince());
        old.setCity(address.getCity());
        old.setCounty(address.getCounty());
        old.setZipcode(address.getZipcode());
        old.setEmail(address.getEmail());
        old.setAddress(address.getAddress());
        addressDao.updateAddress(old);

        if (old.isDefault() != address.isDefault() && address.isDefault()) {
            addressDao.setDefaultAddress(old.getCustomerId(), old.getType(), old.getId());
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void setDefaultAddress(long customerId, String type, long id)
            throws CustomerException
    {
        addressDao.setDefaultAddress(customerId, type, id);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void removeAddress(long... ids)
            throws CustomerException
    {
        for (long id : ids) {
            CustomerAddress address = addressDao.getAddress(id);
            if (address != null) {
                addressDao.removeAddress(address);
            }
        }
    }

    public CustomerAddress getAddress(long id)
    {
        return addressDao.getAddress(id);
    }

    public List<CustomerAddress> listAddress(long customerId, String type)
    {
        Criteria criteria = new Criteria();
        if (StringUtils.isNotBlank(type)) {
            criteria.add(Restrictions.eq("type", type));
        }
        criteria.addOrder(Order.desc("id"));
        return addressDao.listAddress(customerId, criteria);
    }

    public PagingList<CustomerAddress> queryAddress(long customerId, Criteria criteria)
    {
        return addressDao.queryAddress(customerId, criteria);
    }

    public PagingList<CustomerAddress> queryAddress(Criteria criteria)
    {
        return addressDao.queryAddress(criteria);
    }
}
